import config from '@/common/config'
import tools from '@/common/tools'
export  default {
    checkSession(){
        return new Promise(resolve => {
            uni.checkSession({
                success:res=>{
                    resolve(true)
                },
                fail:res=>{
                    resolve(false)
                }
            })
        })
    },
    login(){
        return new Promise(resolve => {
            uni.login({
                provider: 'weixin',
                success: res => {
                    resolve(res.code)
                },
                fail:res=>{
                    resolve(false)
                }
            })
        })
    },
    getSystemInfo: function getSystemInfo() {
        return new Promise(function (resolve, reject) {
            uni.getSystemInfo({
                success: function success(e) {
                    resolve(e);
                },
                fail: function fail(error) {
                    reject(error);
                } });


        });
    },
    getImageInfo(url){
        return new Promise(function (resolve, reject) {
            uni.getImageInfo({
                src: url,
                success: res => {
                    resolve(res);
                },
                fail:error=>{
                    reject(error)
                }
            })
        })

    },
    downloadFile(url){

        return new Promise((resolve, reject) => {
            uni.downloadFile({
                url: url,
                success(res) {
                    if (res.statusCode === 200) {
                        resolve(res.tempFilePath);
                    }else{
                        reject(false);
                    }
                }
            });
        })
    },
    openSetting(scope){
        return new Promise(resolve => {
            uni.openSetting({
                success:res=>{
                    if (res.authSetting[`scope.${scope}`]) {
                        resolve(true)
                    } else {
                        resolve(false)
                    }
                }
            })
        })
    },
    authorize(scope){
        return new Promise(resolve => {
            uni.authorize({
                scope: `scope.${scope}`,
                success() {
                    resolve(true)
                },
                fail:error=>{
                    resolve(false)
                }
            })
        })
    },
    getSetting(scope){
        return new Promise(resolve => {
            uni.getSetting({
                success:res=>{
                    if (res.authSetting[`scope.${scope}`]) {
                        console.log(scope+"用户授权了");
                        resolve(true)
                    } else {
                        //用户没有授权
                        console.log("用户没有授权");
                        resolve(false)
                    }
                }
            })
        })
    },
    makePhoneCall(phone){
        return new Promise(resolve => {
            wx.makePhoneCall({
                phoneNumber: phone,
                success(res) {
                    resolve(true)
                }
            })
        })
    },
    getLocation(){
        return new Promise(resolve => {
            uni.getLocation({
                type: 'wgs84',
                success (res) {
                    console.log('获取地理位置成功');
                    console.log(res);
                    resolve(res)
                },
                fail:error=>{
                    console.log(error)
                }

            });
        })
    },
    chooseLocation(){
        return new Promise(resolve => {
            uni.chooseLocation({
                success: function (res) {
                    console.log(res);
                    resolve(res)
                },
            })
        })
    },
    openLocation(location){
        return new Promise(resolve => {
            console.log(location)
            uni.openLocation(Object.assign(location,{
                success:res=>{
                    resolve(res)
                }
            }))
        })
    },
    showModal(title,content){
        return new Promise(resolve => {
            uni.showModal({
                title:title,
                content:content,
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击确定');
                        resolve(true)
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                        resolve(false)
                    }
                }
            })
        })
    },
    pay(data){
        return new Promise(function (resolve, reject) {
            wx.requestPayment({
                timeStamp: data.timestamp,
                nonceStr:  data.nonceStr,
                package:   data.package,
                signType: 'MD5',
                paySign:   data.paySign,
                success:function (res) {
                    resolve(res);
                },
                fail:function (res) {
                    resolve(res);
                }
            })
        });
    }








}


